import Vue from 'vue'
import VueRouter from 'vue-router'
import Homemain from '@/views/homemain/Homemain.vue'
import Home from '@/views/home/Home.vue'
import Mine from '@/views/mine/Mine.vue'
import Login from '@/views/login/Login.vue'
// 校历页面
import Calendar from '@/views/calendar/calendar.vue'
import Testing from '@/views/testing/Testing.vue'
import Schedul from '@/views/schedul/Schedul.vue'
import Leave from '@/views/leave/Leave.vue'
import Detail from '@/views/detail/detail.vue'
import { Dialog } from 'vant';

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/homemain'},
  { path: '/homemain', component: Homemain,children:[
    { path: '/', redirect: '/homemain/home',},
    { path: 'home', component: Home },
    { path: 'mine', component: Mine },
  ]},
  { path: '/login', component: Login },
  { path: '/calendar', component: Calendar },
  { path: '/testing', component: Testing },
  { path: '/schedul', component: Schedul },
  { path: '/leave', component: Leave },
  { path: '/detail', component: Detail },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from,next) => {
  const checkRoute = ['/leave','/testing','/schedul']
  if(checkRoute.indexOf(to.path)!==-1){
    const token=localStorage.getItem('Token');
    if(!token){
      Dialog.confirm({title: '打开失败',message: '您尚未登录，请先登录',})
      .then(() => {
        next('/login');
      })
      .catch(() => {
        next(false);
      });
    }else{
      next();
    }
  }else{
    next();
  }
})

export default router
